package org.example.callback;

import com.alibaba.fastjson2.JSONObject;
import com.sun.jna.Pointer;
import com.wp.domain.req.UserInfoSearchReq;
import com.wp.domain.resp.UserInfoSearchResp;
import com.wp.properties.Device;
import com.wp.service.DeviceApp;
import com.wp.service.HCNetSDK;
import com.wp.service.HCNetSDKApp;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

@Slf4j
public class HuaNengCallback implements HCNetSDK.FMSGCallBack_V31 {

    /**
     * 事件监听的回调
     *
     * @param lCommand   上传的消息类型，不同的报警信息对应不同的类型，通过类型区分是什么报警信息
     *                   明眸门禁：
     *                   COMM_ALARM_ACS 门禁主机报警信息
     *                   COMM_ID_INFO_ALARM 门禁身份证刷卡信息
     *                   COMM_PASSNUM_INFO_ALARM 门禁通行人数信息
     * @param pAlarmer   报警设备信息，包括设备序列号、IP地址、登录IUserID句柄等
     * @param pAlarmInfo 报警信息，通过lCommand值判断pAlarmer对应的结构体
     * @param dwBufLen   用户数据
     * @param pUser      报警信息缓存大小
     * @return
     */
    @Override
    public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        // 用户句柄
        int lUserID = pAlarmer.lUserID;
        System.out.println("lCommand = 0x" + Integer.toHexString(lCommand));

        StringBuilder stringBuilder = new StringBuilder();
        if (lCommand == HCNetSDK.COMM_ALARM_ACS) {
            stringBuilder.append("门禁主机报警:");
            HCNetSDK.NET_DVR_ACS_ALARM_INFO netDvrAcsAlarmInfo = new HCNetSDK.NET_DVR_ACS_ALARM_INFO();
            netDvrAcsAlarmInfo.useMemory(pAlarmInfo);
            netDvrAcsAlarmInfo.read();

            short wInductiveEventType = netDvrAcsAlarmInfo.wInductiveEventType;
            if (wInductiveEventType == 0) {
                System.out.println("dwMajor = 0x" + Integer.toHexString(netDvrAcsAlarmInfo.dwMajor));
                System.out.println("dwMinor = 0x" + Integer.toHexString(netDvrAcsAlarmInfo.dwMinor));

                if (netDvrAcsAlarmInfo.dwMajor == 0x5 && netDvrAcsAlarmInfo.dwMinor == 0x4b) {
                    stringBuilder.append("有效报警:事件:人脸认证通过:");
                    facePassed(lUserID, netDvrAcsAlarmInfo);
                }
            }
        }
        log.info("回调事件通知 {}", stringBuilder);
        return true;
    }

    private void facePassed(int lUserID, HCNetSDK.NET_DVR_ACS_ALARM_INFO netDvrAcsAlarmInfo) {
        if (netDvrAcsAlarmInfo.byAcsEventInfoExtend == 1) {
            // 收集并保存认证成功的人脸信息
            HCNetSDK.NET_DVR_ACS_EVENT_INFO_EXTEND netDvrAcsEventInfoExtend = new HCNetSDK.NET_DVR_ACS_EVENT_INFO_EXTEND();
            netDvrAcsEventInfoExtend.useMemory(netDvrAcsAlarmInfo.pAcsEventInfoExtend);
            netDvrAcsEventInfoExtend.read();
            // 读取用户工号
            String byEmployeeNo = new String(netDvrAcsEventInfoExtend.byEmployeeNo, StandardCharsets.UTF_8).trim();

            UserInfoSearchReq req = new UserInfoSearchReq();
            req.setSearchID(byEmployeeNo);
            req.setEmployeeNoList(byEmployeeNo);
            UserInfoSearchResp userInfoSearchResp = HCNetSDKApp.User.search(lUserID, req);

            System.out.println(JSONObject.toJSONString(userInfoSearchResp));
//            if (userInfoSearchResp != null && userInfoSearchResp.getUserInfo() != null && !userInfoSearchResp.getUserInfo().isEmpty()) {
//                UserInfoSearchResp.UserInfo userInfo = userInfoSearchResp.getUserInfo().get(0);
//                // 保存用户信息到数据库中
////                UserDao userDao = BeanUtil.getBean("userDao", UserDao.class);
//                EnterRecord enterRecord = new EnterRecord();
//                enterRecord.setStaffNo(userInfo.getEmployeeNo()); // 员工编号
//                enterRecord.setStaffName(userInfo.getName()); // 员工姓名
//                enterRecord.setEnterTime(DateUtils.format(new Date())); // 进入时间
//                enterRecord.setFaceControlTag("in"); // 入库
//                System.out.println(JSONObject.toJSONString(enterRecord));
////                userDao.addUserEnterRecord(enterRecord);
//            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        // SDK初始化
        HCNetSDKApp.initSDKInstance();
        // 设置登录信息
        Device device = new Device("192.168.2.229", (short) 8000, "admin", "hn123456");
        DeviceApp deviceApp = HCNetSDKApp.deviceLogin(device);

        // 设置回调信息
        HCNetSDKApp.Event.setCallback(new HuaNengCallback(), null);
        // 布防
        HCNetSDKApp.Event.setAlarm(deviceApp);
        while (true) {
            //这里加入控制台输入控制，是为了保持连接状态，当输入Y表示布防结束
            System.out.print("请选择是否撤出布防(Y/N)：");
            Scanner input = new Scanner(System.in);
            if (input.next().equals("Y")) {
                break;
            }
        }
        // 注销
        deviceApp.logout();
    }
}
